<template>
  <view class="contain">
    <view>
      <u-toast ref="uToast" />
    </view>
    <view class="top-content"></view>

    <view class="top" >
      <u-input v-model="value" suffixIcon="search" :border="true" @iconClick="onClick" placeholder="请输入设备名称或序列号" :clearable="true" />
      <u-button
        type="primary"
        size="mini"
        class="btn"
        style="margin-left:5rpx;margin-top:5rpx"
      >重置</u-button>
    </view>

  <view class="box-contet"  v-for="(item, index) in dataList" :key="index" style="padding:0rpx 30rpx">
    
               <!-- <u-swipe-action
          v-for="(item, index) in dataList"
          :key="index"
          :index="index"
          @click="click"
          @open="open"
          :options="options"
          :show="item.show"
          class="card1"
          style="width: 100%;"
        > -->
        <view style="position: relative;">
          <card 
            :data="item"
            :id="taskId"
            :timeone="timeone"
            :hour="hour"
            :minute="minute"
            :second="second"
            class="color"
            style="width: 100%;"
          />
          <view class="bottombox" @click.stop='editClick(item)' > 
  <text class="sumarry" style="font-size:20rpx;">编辑</text>
</view>
<view class="bottombox1" @click.stop='deleteClick(item)'  > 
 <text style="font-size:20rpx">删除</text>
</view>
          </view>
             
  

        <!-- </u-swipe-action> -->
         <u-loadmore :status="status" :icon-type="iconType" :load-text="loadText" />

    </view>
 
 

    <view class="fix" @click="handleShowSec">
      <image src="../../static/images/saomiao.png" />
      <!-- <u-icon name="scan" color="white" size="66"></u-icon> -->
    </view>



    <view class="bottom">
      <u-popup v-model="hidden" :mask-close-able="false" mode="center" border-radius="14" width="600rpx" height="200px">
        <view class="title">{{editdevice == true ? '修改序列号' : '确认设备'}}</view>
        <view class="input">
          <u-form-item label-width="100" label="序列号">
            <u-input v-model="sn" :focus="true" :placeholder="search" />
          </u-form-item>
        </view>
        <view class="foot">
          <view class="btn">
            <u-button @click="back" :ripple="true" ripple-bg-color="#32afff">返回</u-button>
          </view>
          <view class="btn">
            <u-button @click="handleGo" type="primary" :ripple="true" ripple-bg-color="#32afff">确认</u-button>
          </view>
        </view>
      </u-popup>
    </view>

    <view>
      <u-modal
        v-model="show"
        :show-title="false"
        :show-cancel-button="true"
        @confirm="confirm"
        @cancel="cancel"
        :content="content"
      ></u-modal>
    </view>


  </view>
</template>

<script>
import card from "@/components/product/card.vue";
import {
  pointCheckTaskDevice,
  pointCheckTaskDeviceAdd,
  pointCheckTaskDeviceEdit,
  pointCheckTaskDeviceRemove
} from "@/api/index.js";
export default {
  components: {
    card
  },
  onLoad(val) {
    this.taskId = val.data;
    this.timeone = val.timeone;
    this.hour = val.hour;
    this.minute = val.minute;
    this.second = val.second;
  },
  onShow() {
    this.getList();
  },
  data() {
    return {
      search: "请扫描序列号",
      datashow: false,
      status: "nomore",
      iconType: "flower",
      loadText: {
        loadmore: "暂无数据",
        loading: "努力加载中",
        nomore: "没有更多数据"
      },
      startdata: 0,
      enddata: 0,
      show: false,
      content: "是否确认删除？",
      oldIndex: "",
      sn: null,
      src: "../../static/images/nodata.png",
      taskId: "",
      id: "",
      show: false,
      editdevice: false,
      newIndex: -1,
      loadingShow: false,
      showFirst: false,
      hidden: false,
      value: null,
      circle: true,
      status: "loading",
      options: [
        {
          text: "修改",
          style: {
            background: "linear-gradient(to left, #1040be, #026dff)"
            // marginLeft:'-20rpx'
          }
        },
        {
          text: "删除",
          style: {
            background: "linear-gradient(to left, #FF0000, #FF9494)"
            // marginLeft: '10rpx'
          }
        }
      ],
      dataList: []
    };
  },
  watch: {
    value: {
      // 可以监听对象的单个属性
      handler(newval, oldval) {
        this.getList();
      },
      deep: true
    }
  },
  onHide() {
    this.hidden = false;
    this.show = false;
    this.showFirst = false;
  },
  // 触碰底部
  onReachBottom() {
    this.status = "loading";
    this.enddata = this.enddata + 5;
    this.getList();
  },
  methods: {
    handleShow() {
      this.show = true;
    },
    hanleClose() {
      this.show = false;
    },
    // 扫描按钮
    handleShowSec() {
      this.dataList.map((val, idx) => {
        this.dataList[idx].show = false;
      });
      if (this.editdevice != true) {
        this.sn = "";
      }
      this.hidden = true;
    },

    hanleCloseSec() {
      this.showFirst = false;
    },
    getList() {
      this.datashow = false;
      uni.showLoading({
        title: "加载中"
      });
      // const data = {
      //   startdata: 0,
      //   enddata: this.enddata + 6
      // }
      const data2 = {
        pageNum: 0,
        pageSize: this.enddata + 4,
        taskId: this.taskId,
        sn: this.value
      };
      pointCheckTaskDevice(data2).then(res => {
        if (res.total > data2.pageSize) {
          this.status = "loading";
        } else if (res.total == 0) {
          this.status = "loadmore";
          this.datashow = true;
        } else {
          this.status = "nomore";
        }
        res.rows.map(item => {
          let show = "show";
          item[show] = false;
        });
        // const newdata = res.rows.slice(data.startdata, data.enddata - 1)
        this.dataList = res.rows;
        uni.hideLoading();
      });
    },
    handleScan() {
      uni.scanCode({
        success: function(res) {
          uni.navigateTo({
            url: "/pages/detail/detail"
          });
        }
      });
    },
    // 返回
    back() {
      this.search = "请扫描序列号";
      this.hidden = false;
      this.editdevice = false;
      this.dataList.map((val, idx) => {
        this.dataList[idx].show = false;
      });
    },
    handOpen() {
      this.hidden = true;
    },
    // 确认按钮
    handleGo() {
      if (this.sn) {
        console.log(1111);
        if (this.editdevice) {
          const data = {
            id: this.id,
            taskId: this.taskId,
            sn: this.sn
          };
          pointCheckTaskDeviceEdit(data).then(res => {
            this.getList();
            this.loadingShow = false;
            this.sn = "";
            this.$refs.uToast.show({
              title: "修改成功",
              type: "success",
              url: "/pages/user/index"
            });
          });
          this.dataList.map((val, idx) => {
            this.dataList[idx].show = false;
          });
          this.hidden = false;
          this.loadingShow = true;
        } else {
          const data = {
            taskId: this.taskId,
            sn: this.sn
          };
          pointCheckTaskDeviceAdd(data).then(res => {
            this.getList();
            this.loadingShow = false;
            this.sn = "";
            this.$refs.uToast.show({
              title: "添加成功",
              type: "success",
              url: "/pages/user/index"
            });
            this.hidden = false;
            this.editdevice = false;
          });
        }
        this.editdevice = false;
      } else {
        this.search = "序列号不能为空";
      }
      // uni.navigateTo({
      // 	url: '/pages/detail/detail'
      // })
    },
    click(index, index1) {
      if (index1 == 1) {
        this.show = true;
        this.index = index;
        console.log(11111);
      } else {
        this.dataList[index].sn;
        console.log(this.dataList[index].sn);
        this.sn = this.dataList[index].sn;
        this.id = this.dataList[index].id;
        this.editdevice = true;
        this.hidden = true;
      }
    },
    deleteClick(data) {
      this.deleteid = data.id;
      //  console.log(this.deleteid)
      this.show = true;
    },
    editClick(data) {
      console.log(data);
      this.sn = data.sn;
      this.id = data.id;
      this.editdevice = true;
      this.hidden = true;
    },
    confirm() {
      const id = this.deleteid;
      pointCheckTaskDeviceRemove(id).then(res => {
        this.dataList.map((val, idx) => {
          this.dataList[idx].show = false;
        });
        this.getList();
        this.$refs.uToast.show({
          title: "删除成功",
          type: "success",
          url: "/pages/user/index"
        });
      });
    },
    cancel() {
      this.dataList.map((val, idx) => {
        this.dataList[idx].show = false;
      });
    },
    open(index) {
      this.dataList[index].show = true;
      this.dataList.map((val, idx) => {
        if (index != idx) this.dataList[idx].show = false;
      });
    },
    onClick() {
      this.getList();
    }
  }
};
</script>
<style lang="scss" scoped>
.contain {
  width: 100%;
  height: 100%;
  position: relative;
  .top-content {
    height: 145upx;
    background: linear-gradient(to left, #1040be, #026dff);
  }
}
.top-content {
  height: 160upx;
  background: linear-gradient(to left, #1040be, #026dff);
}
.top {
  z-index: 10;
  position: absolute;
  padding: 30rpx 30rpx 18rpx 30rpx;

  width: 100%;
  top: 50rpx;
  background-color: white;
  display: flex;
  border-radius: 40rpx 40rpx 0 0;

  .btn {
    position: absolute;
    right: 35rpx;
    border-radius: 16rpx;
  }
  /deep/ .u-btn--primary {
    background: linear-gradient(to left, #1040be, #026dff);
  }
  /deep/ .uni-input-placeholder {
    font-size: 22rpx;
    color: #989898 !important;
    // line-height:100upx;
  }
  /deep/ .u-mode-plain-primary {
    border: 1px solid #eee;
    box-shadow: 0 0 20rpx #eee;
    border-radius: 16rpx;
    color: #989898;
  }
  /deep/ .u-flex {
    margin-right: 80rpx;
  }
  .image {
    position: absolute;
    width: 28rpx;
    height: 34rpx;
    left: 45rpx;
    // height: 80upx;
  }
  /deep/ .u-size-default {
    padding: 18rpx 24rpx 18rpx 50rpx;
  }
  /deep/ .u-input--border {
    border: 1px solid #eee !important;
    box-shadow: 0 0 20rpx #eee;
    border-radius: 16rpx;
    // color: #989898 !important;
    // height: 60upx;
    // line-height: 20upx;
  }
  /deep/ .u-input__input {
    min-height: 55upx !important;
    color: #989898 !important;
  }
}
.contain {
  // .top {
  //   height: calc(100% - 50upx);
  //  overflow-y: auto;
  // position: absolute;
  // background-color: white;
  // // padding: 30rpx 0rpx;
  // width: 100%;
  // top: 50rpx;
  // border-radius: 40rpx 40rpx 0 0;
  //   /deep/ .is-input-border {
  //     border: 1px solid #eee !important;
  //     box-shadow: 0 0 20rpx #eee;
  //     border-radius: 16rpx;
  //   }
  //   /deep/ .uni-easyinput {
  //     min-height: 55upx !important;
  //     color: #989898 !important;
  //   }
  //   /deep/ .uni-easyinput__content-input {
  //     font-size: 24rpx;
  //     color: #989898 !important;
  //   }
  //   /deep/ .uni-input-placeholder {
  //     font-size: 24rpx;
  //     color: #989898 !important;
  //   }
  // }
  .card {
    width: 100%;
    margin-top: 84rpx;
  }
  .body {
    .text {
      position: relative;
      // margin-top: 10rpx;
      padding-top: 10rpx;
      height: 60rpx;
      line-height: 60rpx;
      text-align: center;
      font-size: 34rpx;
      .right {
        position: absolute;
        top: 6px;
        right: 30rpx;
        color: #777;
      }
    }
    .photo {
      // margin-top: 50rpx;
      padding-top: 60rpx;
      display: flex;
      height: 200rpx;

      .cont {
        flex: 1;
        .center {
          text-align: center;
        }
        .text {
          text-align: center;
          font-size: 28rpx;
          color: #111;
        }
      }
    }
  }
  .box-contet {
    width: 100%;
    height: calc(100% - 180upx);
    overflow-y: auto;
    margin-top: 20rpx;
    .cardlist {
      height: 100%;
      .swiper-item {
        height: 100%;
      }
    }
  }
  .list {
    width: 100%;
    padding: 30rpx;
    .color {
      // border-bottom: 15rpx solid #eee;
    }
  }
  .bottom {
    .title {
      font-size: 18px;
      font-weight: 600;
      text-align: center;
      margin-top: 10px;
    }
    .input {
      width: 100%;
      padding: 20rpx 50rpx 10rpx;
    }
    .foot {
      display: flex;
      .btn {
        flex: 1;
        margin: 60rpx 50rpx 0rpx;
      }
    }
  }
  // .fixs {
  //   position: fixed;
  //   bottom: 100rpx;
  //   right: 40rpx;
  //   width: 120rpx;
  //   height: 120rpx;
  //   background: #333;
  //   background: linear-gradient(-34deg, #1040be, #026dff);
  //   // opacity: 0.3;
  //   border-radius: 50%;
  //    display: flex;
  //    justify-content: center;
  //    align-items: center;
  // }
  .fix {
    position: fixed;
    bottom: 100rpx;
    right: 40rpx;
    z-index: 9999;
    image {
      height: 120rpx;
      width: 120rpx;
    }
  }
}
/deep/ .u-swipe-action {
  // position: relative;
}
/deep/ .u-swipe-content {
  // width:1200rpx
}
/deep/ .u-swipe-del {
  // overflow: hidden;
  // right: -20rpx;
  // position: relative;
  // width:50rpx !important;
  left: 4rpx;
  // height: 280rpx;
  margin-bottom: 20rpx;
  // top: 80rpx;
  // margin-right: 10rpx;
  border-radius: 25rpx;
}
/deep/ .u-swipe-view {
  width: 100%;
  // background-color: red;
}

/deep/ .u-mode-center-box {
  height: initial !important;
}
/deep/ .uni-scroll-view {
  height: initial !important;
  margin: 10px 4px;
}
/deep/ .content-clear-icon {
  font-size: 32rpx;
}
/deep/ .uni-easyinput__content {
  background-color: white;
}
.bottombox {
  margin-top: 15rpx;
  width: 100%;
  height: 50rpx;
  position: absolute;
  background-color: rgba(16, 91, 245, 74%);
  border-radius: 20px;
  color: white;
  width: 170rpx;
  display: flex;
  align-items: center; /*垂直居中 */
  justify-content: center;
  right: 64%;
  bottom: 8.5%;
}

.bottombox1 {
  margin-top: 15rpx;
  width: 100%;
  height: 50rpx;
  position: absolute;
  background-color: rgba(16, 91, 245, 74%);
  border-radius: 20px;
  color: white;
  width: 170rpx;
  display: flex;
  align-items: center; /*垂直居中 */
  justify-content: center;
  right: 34%;
  bottom: 8.5%;
}

.bottombox2 {
  margin-top: 15rpx;
  width: 100%;
  height: 50rpx;
  position: absolute;
  background-color: rgba(16, 91, 245, 74%);
  border-radius: 20px;
  color: white;
  width: 170rpx;
  display: flex;
  align-items: center; /*垂直居中 */
  justify-content: center;
  right: 37%;
  bottom: 14%;
}
</style>
